//
//  PopoverPresentationController.swift
//  XNWeiBo_Swift
//
//  Created by xunan on 16/6/22.
//  Copyright © 2016年 xunan. All rights reserved.
//

import UIKit

class PopoverPresentationController: UIPresentationController {
    
    // 定义属性 保存大小
    var presentFrame = CGRectZero
    
    /**
     专场动画
     
     - parameter presentedViewController:  <#presentedViewController description#>
     - parameter presentingViewController: <#presentingViewController description#>
     
     - returns: <#return value description#>
     */
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController?) {
        
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    }
    
    override func containerViewWillLayoutSubviews() {
        // 修改弹出视图大小
        if presentFrame == CGRectZero {
            //        containerView()
            presentedView()?.frame = CGRect(x: UIScreen.mainScreen().bounds.size.width/2 - 100, y: 64, width: 200, height: 200)
        } else {
            presentedView()?.frame = presentFrame
        }
        
        containerView?.insertSubview(converView, atIndex: 0)
    }
    private lazy var converView: UIView = {
        
        let view = UIView()
        view.backgroundColor = UIColor(white: 0.0, alpha: 0.1)
        view.frame = UIScreen.mainScreen().bounds
        
        let tap = UITapGestureRecognizer(target: self, action: #selector(PopoverPresentationController.close))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    func close() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
}
